from myelin.server.nodetype.Events import EventHandler 
from nodetypes.services.versa.UrlService import Service as VersaService
from nodetypes.services.RdfQueryConfig import Serializer as ConfigSerializer


class EventHandler(EventHandler):
    
    newQueryService = VersaService
    newConfigSerializer = ConfigSerializer
    newDocument = None
            
    def process(self, context, source):
        eventData = context.event.data
        eventData.config = self.newConfigSerializer().deserialize(source.asInputSource())
        if self.newQueryService().execute(context, source):            
            eventData.status = u'success'
            eventData.status_type = u'versa-success'
            
            return self.newDocument().createDocument(
                context,
                source
            )
        
        else:
            from myelin.server.lib.http.Exceptions import Exception
            from myelin.server.lib.http.Exceptions import Error
            
            raise Exception(
                Error.BAD_REQUEST_PARAMS,
                eventData = context.event.data
            )